package com.tanhua.dubbo.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.mapper.QuestionMapper;
import com.tanhua.dubbo.pojo.Question;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService(version = "1.0.0")
@Slf4j
public class QuestionApiImpl implements QuestionApi {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public List<Question> queryQuestion(Long userId) {
        return this.questionMapper.selectList(new QueryWrapper<Question>().eq("user_id", userId));
    }

    @Override
    public Boolean saveQuestions(Long userId, String content) {
        //查询问题是否已经存在
        List<Question> questionList = this.queryQuestion(userId);

        if(questionList.size()>1){
            log.debug("查询多条问题");
            return false;
        }

        if(questionList.size()>0){
            //一条，更新问题
            Question question = questionList.get(0);
            question.setTxt(content);
         return    this.questionMapper.update(question,new UpdateWrapper<Question>().eq("user_id",userId))==1;

        }else{
            Question question = new Question();
            //问题为空，创建新的问题
            question.setTxt(content);
            question.setUserId(userId);
            return   this.questionMapper.insert(question)==1;
        }
    }
}
